﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CefSharp;
using CefSharp.Wpf;
using KeZhou.ViewModels;
using KeZhou.Views.Components.Controls;
using KeZhou.Views;

namespace KeZhou.Views.Components.Controls
{
    /// <summary>
    /// MainTwoChooseCity.xaml 的交互逻辑
    /// </summary>
    public partial class MainTwoChooseCity : Page
    {
        private static MainTwoChooseCity instance;
        private CityInfoCard infoCard;

        public static MainTwoChooseCity Instance
        {
            get
            {
                return instance ?? (instance = new MainTwoChooseCity());
            }
        }
        public MainTwoChooseCity()
        {
            InitializeComponent();
            instance = this;
            this.DataContext = ChooseCityViewModel.Instance;

            // 允许以同步的方式注册C#的对象到JS中
            Browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            // 禁用函数名变换
            Browser.JavascriptObjectRepository.NameConverter = null;

            // 注册C#对象到JS中
            Browser.JavascriptObjectRepository.Register("ViewModel", ChooseCityViewModel.Instance, options: BindingOptions.DefaultBinder);

            ChooseCityViewModel.Instance.setBrowser(Browser);
        }

        public void Browser_Loaded(object sender, RoutedEventArgs e)
        {
            Browser.Load(Environment.CurrentDirectory + "/Map/ChooseCity/map.html");
        }

        public void showInfoCard()
        {
            if (infoCard != null && infoCard.IsLoaded)
            {
                return;
            }
            infoCard = new CityInfoCard();
            infoCard.Owner = AppDetailView.Instance;
            infoCard.DataContext = ChooseCityViewModel.Instance;
            infoCard.Show();
        }
        
        public void closeInfoCard()
        {
            infoCard.Close();
        }

        public void showMessage(string title, string content)
        {
            this.MessageHost.pushMessage(title, content);
        }
    }
}
